/* eslint-disable array-callback-return */
/* eslint-disable no-param-reassign */
/* eslint-disable no-unused-vars */
import { message } from 'antd';
import { getRedEmpDetail } from '@/services/redass';

export default {
  state: {
    sendId: null,
    responseDeduAssBaseDto: {}, // 基本信息
    responseDeduAssSendDto: {}, // 派单信息
    responseDeduAssDto: {}, // 已经减少的险种
    responseDeduInfo: {}, // 减员信息、
    rsponseDeduPdDto: {}, // 产品信息
    handDto: [], // 	办理备注
    probDets: [], // 问题记录
    recordList: [], // 联系记录
    dataDtos: [], // 材料信息
    execStatusDto: {}, // 执行状态
  },
  effects: {
    // 详情数据
    *fetchRedEmpDetail({ payload }, { call, put, select }) {
      const response = yield call(getRedEmpDetail, payload);
      const pdDetailDtos = [];
      const expandedRowKeys = [];
      if (response.success) {
        if (
          response.data.rsponseDeduPdDto &&
          response.data.rsponseDeduPdDto.pdDetailDtos &&
          response.data.rsponseDeduPdDto.pdDetailDtos.length
        ) {
          response.data.rsponseDeduPdDto.pdDetailDtos.map((item, index) => {
            if (item.deduTaskDets && item.deduTaskDets.length) {
              item.deduTaskDets.map((ass, i) => {
                ass.pdName = ass.assName;
                ass.id = `${index}-${i}`;
              });
            }
            item.id = `${index}`;
            if (item.deduTaskDets) {
              item.children = item.deduTaskDets || [];
              expandedRowKeys.push(item.id);
            }
          });
        }
        const sortNumber = (a, b) => a.operLeafType - b.operLeafType;
        if (response.data.execStatusDto && response.data.execStatusDto.leafList) {
          response.data.execStatusDto.leafList = response.data.execStatusDto.leafList.sort(
            sortNumber
          );
        }
        yield put({
          type: 'updateModelData',
          payload: {
            ...payload,
            ...response.data,
            expandedRowKeys,
          },
        });
      } else {
        message.warning(response.message || '获取人员信息：服务端错误');
      }
    },
  },

  reducers: {
    // 设置修改当前state数据
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
